#include "com_key_KeyChineseChessView.h"
#include "define.h"
#include "Eveluation.h"
#include "AlphaBetaEngine.h"
#include "SearchEngine.h"
#include "NegaScout_TT_HH.h"
#include "MTD_f.h"
#include "PVS_Engine.h"

JNIEXPORT void JNICALL Java_com_key_KeyChineseChessView_goAI
	(JNIEnv * env, jobject obj, jobjectArray arr,jint jlevel, jintArray movearr, jboolean isrule)
{
	int position[10][9];
	int size = env->GetArrayLength(arr);
	int level = (int)jlevel;
 	jint buf[10][9];
	jint jmoves[4];
	
	for (int i = 0; i < size; i++) 
	{
		jintArray myarray = (jintArray)((env)->GetObjectArrayElement(arr, i));
		env->GetIntArrayRegion(myarray,0,9,buf[i]);
		for (int j = 0; j < 9; j++) {
			jint pos = buf[i][j];
			position[i][j]=pos;
		}
		env->DeleteLocalRef(myarray);
	}
	env->GetIntArrayRegion(movearr,0,4,jmoves);
	moves[0] = (int)jmoves[0];
	moves[1] = (int)jmoves[1];
	moves[2] = (int)jmoves[2];
	moves[3] = (int)jmoves[3];
	is_rule = (bool)isrule;
	
	if(level==1)
	{
		SetSearchDepth(2);
		SearchAGoodMoveNegaSTTHH(position);		
	}
	else if(level==2)
	{
		SetSearchDepth(2);
		SearchAGoodMovePVS(position);
	}
	else if(level==3)
	{
		SetSearchDepth(3);
		SearchAGoodMoveMTDf(position);
	}
	else if(level==4)
	{
		SetSearchDepth(3);
		SearchAGoodMoveNegaSTTHH(position);
	}
	else if(level==5)
	{
		SetSearchDepth(3);
		SearchAGoodMovePVS(position);
	}
	else if(level==6)
	{
		SetSearchDepth(4);
		SearchAGoodMoveNegaSTTHH(position);
	}
	else if(level==7)
	{
		SetSearchDepth(4);
		SearchAGoodMovePVS(position);
	}
	else if(level==8)
	{
		SetSearchDepth(5);
		SearchAGoodMoveMTDf(position);
	}
	else if(level==9)
	{
		SetSearchDepth(5);
		SearchAGoodMoveNegaSTTHH(position);
	}
	else if(level==10)
	{
		SetSearchDepth(6);
		SearchAGoodMoveNegaSTTHH(position);
	}
	if(level!=2&&level!=5&&level!=7)
	{
		for(int i=0;i<10;i++)
		{
			delete m_pTT[i];
		}
	}
	jmoves[0] = (jint)moves[0];
	jmoves[1] = (jint)moves[1];
	jmoves[2] = (jint)moves[2];
	jmoves[3] = (jint)moves[3];
	env->SetIntArrayRegion(movearr,0,4,jmoves);
	for (int i = 0; i < size; i++) 
	{
		jintArray myarray = (env)-> NewIntArray(9); 
		jint* val = (jint *)position[i];
		env->SetIntArrayRegion(myarray,0,9,val);
		//= (jintArray)((env)->GetObjectArrayElement(arr, i))
		env->SetObjectArrayElement(arr,i,myarray);
		env->DeleteLocalRef(myarray);
	}
}